package org.goldenport.g3

/**
 * @since   Jun. 10, 2010
 * @version Oct. 14, 2010
 * @author  ASAMI, Tomoharu
 */
abstract class G3Event(val uri: String, val cause: Option[G3Event] = None) extends G3Message {
  val id = G3Id.createEventId()
  def published = id.time
  var input: Option[Any] = None
  var output: Option[Any] = None
  var inputMessage: Option[G3Message] = None
  var inputParcel: Option[G3Parcel] = None
//  def content = output.get

  // used in G3FileStoreAdapter createFiles/writeFiles
  def setInput[T <: G3Event](value: Any): T = {
    input = make_value(value)
    this.asInstanceOf[T]
  }

  // used in G3FileStoreAdapter createFiles/writeFiles
  def setOutput[T <: G3Event](value: Any): T = {
    output = make_value(value)
    this.asInstanceOf[T]
  }

  private def make_value(value: Any): Option[Any] = {
    value match {
      case None => None
      case _: Unit => None
      case _ => Some(value)
    }
  }

  override protected def property_Sources: List[G3Message] = {
    List(cause, inputMessage) flatten
  }
}

class NoEvent(uri: String, msg: Option[G3Message]) extends G3Event(uri) {
  inputMessage = msg
}

object NoEvent {
  def apply(uri: String) = new NoEvent(uri, None)
  def apply(uri: String, msg: G3Message) = new NoEvent(uri, Some(msg))
}
